<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include "header.php";
$page = "edit_user";
$result = FALSE;
//check user login

if (!$user->user_exists) {
    header('Location: ./login.php');
    exit();
}

if (isset($_POST['user_id'])) {
    $user_id = $_POST['user_id'];
} elseif (isset($_GET['user_id'])) {
    $user_id = $_GET['user_id'];
} else {
    $user_id = "";
}
$error = array();
$userClass = new HOMEUser();
$types = $userClass->getTypes();
$user_edit = $userClass->getAccountById($user_id);

if ($user->user_info['type_id'] != 1 || empty($user_id) || empty($user_edit)) {
    header('Location: ./restrict.php');
    exit();
}
/*
  if ($user->user_info['user_locked']) {
  header('Location: ./locked.php');
  exit();
  } */

if (isset($_POST['email'])) {
    $email = $_POST['email'];
} elseif (isset($_GET['email'])) {
    $email = $_GET['email'];
} else {
    $email = "";
}
if (isset($_POST['password'])) {
    $password = $_POST['password'];
} elseif (isset($_GET['password'])) {
    $password = $_GET['password'];
} else {
    $password = "";
}
if (isset($_POST['confirm_password'])) {
    $confirm_password = $_POST['confirm_password'];
} elseif (isset($_GET['confirm_password'])) {
    $confirm_password = $_GET['confirm_password'];
} else {
    $confirm_password = "";
}
if (isset($_POST['username'])) {
    $username = $_POST['username'];
} elseif (isset($_GET['username'])) {
    $username = $_GET['username'];
} else {
    $username = "";
}
if (isset($_POST['firstname'])) {
    $firstname = $_POST['firstname'];
} elseif (isset($_GET['firstname'])) {
    $firstname = $_GET['firstname'];
} else {
    $firstname = "";
}
if (isset($_POST['lastname'])) {
    $lastname = $_POST['lastname'];
} elseif (isset($_GET['lastname'])) {
    $lastname = $_GET['lastname'];
} else {
    $lastname = "";
}
if (isset($_POST['address'])) {
    $address = $_POST['address'];
} elseif (isset($_GET['address'])) {
    $address = $_GET['address'];
} else {
    $address = "";
}

if (isset($_POST['phone'])) {
    $phone = $_POST['phone'];
} elseif (isset($_GET['phone'])) {
    $phone = $_GET['phone'];
} else {
    $phone = "";
}

if (isset($_POST['type'])) {
    $type = $_POST['type'];
} elseif (isset($_GET['type'])) {
    $type = $_GET['type'];
} else {
    $type = "";
}

//validate values input
$validate = array(
    'email' => array('email' => $email),
    'username' => array('username' => $username),
    'firstname' => $firstname,
    'lastname' => $lastname,
);
if(!empty($password)){
    $validate ['password'] = array('pass' => $password, 'confirm_pass' => $confirm_password);
}
$save = false;
if (isset($_POST['submit'])) {
    $validator = new HOMEValidate();
    $error = $validator->validate($validate);
    if ($userClass->checkEmailExist($email,$user_edit['id'])) {
        $error[] = 'Someone has already registered this email address, please use another one.';
    }

    if (empty($error)) {
        $userClass = new HOMEUser();
        $result = $userClass->update($user_id,$username, $password, $firstname, $lastname, $address, $email, $phone, $type);
        if ($result) {
            $save = true;
        }
    }
}else{
    $email = $user_edit['user_email'];
    $username = $user_edit['user_name'];
    $firstname = $user_edit['user_fname'];
    $lastname = $user_edit['user_lname'];
    $address = $user_edit['user_address'];
    $phone = $user_edit['user_phone'];
    $type = $user_edit['type_id'];
}

$smarty->assign('email', $email);
$smarty->assign('password', $password);
$smarty->assign('confirm_password', $confirm_password);
$smarty->assign('username', $username);
$smarty->assign('firstname', $firstname);
$smarty->assign('lastname', $lastname);
$smarty->assign('address', $address);
$smarty->assign('phone', $phone);

$smarty->assign('types', $types);
$smarty->assign('type', $type);
$smarty->assign('error', $error);
$smarty->assign('save', $save);

include "footer.php";
